package com.demo2do.boyle.backend.web.controller.aduser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.aduser.AduserService;
import com.demo2do.boyle.common.entity.aduser.Aduser;
import com.demo2do.boyle.common.entity.aduser.AduserBalance;
import com.demo2do.boyle.common.entity.aduser.Company;
import com.demo2do.boyle.common.entity.aduser.Profile;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

/**
 * @author mohanlan
 */
@Controller
@RequestMapping("/adusers")
@MenuSetting("menu-aduser")
public class AduserEditController {
	
	@Autowired
	private AduserService aduserService;
	
	/**
	 * display aduser profile panel
	 * 
	 * @param id
	 * @return the profile info edit view
	 */
	@RequestMapping("/{id}/profile")
	public ModelAndView profile(@PathVariable("id") Integer id) {
		
		ModelAndView modelAndView = new ModelAndView("aduser/edit/aduser-profile");
		
		Aduser aduser = aduserService.load(Aduser.class, id);
		
		modelAndView.addObject("aduser", aduser);
		
		return modelAndView;
	}
	
	/**
	 * edit aduser profile
	 * 
	 * @param id
	 * @param profile
	 * @return
	 */
	@RequestMapping(value = {"/{id}/profile"}, method = RequestMethod.PATCH)
	public @ResponseBody String onEditProfile(@PathVariable("id") Integer id, Profile profile) {
		
		Result result = new Result();
		
		Aduser aduser = aduserService.load(Aduser.class, id);	
		aduserService.editProfile(aduser, profile);
		
		result.success().message("修改个人信息成功");
		
		return JsonUtils.toJsonString(result);
	}
	
	
	/**
	 * display aduser company panel
	 * 
	 * @param id
	 * @return the company info edit view
	 */
	@RequestMapping("/{id}/company")
	public ModelAndView company(@PathVariable("id") Integer id) {
		
		ModelAndView modelAndView = new ModelAndView("aduser/edit/aduser-company");
		
		Aduser aduser = aduserService.load(Aduser.class, id);
		
		modelAndView.addObject("aduser", aduser);
		
		return modelAndView;
	}
	
	/**
	 * edit aduser company
	 * 
	 * @param id
	 * @param company
	 * @return
	 */
	@RequestMapping(value = {"/{id}/company"}, method = RequestMethod.PATCH)
	public @ResponseBody String onEditCompany(@PathVariable("id") Integer id, Company company) {
		
		Result result = new Result();
		
		Aduser aduser = aduserService.load(Aduser.class, id);
		aduserService.editCompany(aduser, company);
		
		result.success().message("修改个人信息成功");
		
		return JsonUtils.toJsonString(result);
	}
	
    /**
     * display aduser balance panel
     * 
     * @return
     */
    @RequestMapping("/{id}/balance")
    public ModelAndView balance(@PathVariable("id") Integer id) {

        ModelAndView modelAndView = new ModelAndView("aduser/edit/aduser-balance-edit");
        
        AduserBalance aduserBalance = aduserService.loadBalance(id);
        
        modelAndView.addObject("aduserBalance", aduserBalance);

        return modelAndView;
    }
    
    /**
     * edit aduser balance
     * 
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}/balance", method=RequestMethod.PATCH)
    public @ResponseBody String onEditBalance(@PathVariable("id") Integer id, @RequestParam("deposit") int deposit) {

        Result result = new Result();
        
        Aduser aduser = aduserService.load(Aduser.class, id);
        
		aduserService.editBalance(aduser, deposit);
		
		result.success().message("账户充值成功");

        return JsonUtils.toJsonString(result);
    }

}
